﻿/*
 * Created by SharpDevelop.
 * User: CeCi
 * Date: 02/04/2014
 * Time: 01:58 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data;
using System.Data.OleDb;
using Gestión_UTN.Entities;

namespace Gestión_UTN.daimpl
{
    /// <summary>
    /// Description of materiadaimpl.
    /// </summary>
    public class materiadaimpl : Gestión_UTN.da.materiada
    {
        public materiadaimpl()
        {
        }

        public bool SaveMateria(string nombre, int car_id)
        {
            bool ingresada = false;

            OleDbConnection conex = null;
            OleDbTransaction trans = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                trans = null;

                if (null != conex)
                {

                    string sql = "INSERT INTO mat_materias(mat_nombre, car_id)" +
                                "VALUES(@nombre, @car_id)";

                    conex.Open();
                    trans = conex.BeginTransaction();

                    OleDbCommand insertar = new OleDbCommand(sql, conex, trans);

                    insertar.Parameters.AddWithValue("@nombre", nombre);
                    insertar.Parameters.AddWithValue("@car_id", car_id);

                    insertar.ExecuteNonQuery();

                    trans.Commit();

                    ingresada = true;

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                ingresada = false;
                trans.Rollback();
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return ingresada;
        }

        public bool DeleteMateria(string nombre, int car_id)
        {
            bool eliminada = false;

            OleDbConnection conex = null;
            OleDbTransaction trans = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                trans = null;

                if (null != conex)
                {

                    string sql = "DELETE FROM mat_materias " +
                        "WHERE mat_nombre = @nombre AND car_id = @car_id";

                    conex.Open();

                    trans = conex.BeginTransaction();

                    OleDbCommand eliminar = new OleDbCommand(sql, conex, trans);

                    eliminar.Parameters.AddWithValue("@nombre", nombre);
                    eliminar.Parameters.AddWithValue("@car_id", car_id);

                    eliminar.ExecuteNonQuery();

                    trans.Commit();

                    eliminada = true;

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                eliminada = false;
                trans.Rollback();
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return eliminada;
        }

        public bool ModifyMateria(string nombre_anterior, int car_id_anterior, string nombre, int car_id)
        {
            bool modificada = false;

            OleDbConnection conex = null;
            OleDbTransaction trans = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                trans = null;

                if (null != conex)
                {

                    string sql = "UPDATE mat_materias SET mat_nombre = @nombre," +
                                    " car_id = @car_id " +
                                    "WHERE mat_nombre = @nombre_anterior AND car_id = @car_id_anterior";

                    conex.Open();
                    trans = conex.BeginTransaction();

                    OleDbCommand modificar = new OleDbCommand(sql, conex, trans);

                    modificar.Parameters.AddWithValue("@nombre", nombre);
                    modificar.Parameters.AddWithValue("@car_id", car_id);
                    modificar.Parameters.AddWithValue("@nombre_anterior", nombre_anterior);
                    modificar.Parameters.AddWithValue("@car_id_anterior", car_id_anterior);

                    modificar.ExecuteNonQuery();

                    trans.Commit();

                    modificada = true;

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                modificada = false;
                trans.Rollback();
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return modificada;
        }

        public System.Data.DataTable FindAllMaterias()
        {
            DataTable tablaMaterias = null;

            OleDbConnection conex = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                tablaMaterias = new DataTable();

                if (null != conex)
                {

                    string sql = "SELECT mat_id, mat_nombre, car_nombre" +
                                    " FROM mat_materias mat, car_carreras car " +
                                    "WHERE mat.car_id = car.car_id";

                    OleDbDataAdapter adaptador = new OleDbDataAdapter(sql, conex);

                    conex.Open();

                    adaptador.Fill(tablaMaterias);

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                tablaMaterias = null;
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return tablaMaterias;
        }


        public Materia FindMateriaByMatId(int mat_id)
        {
            Materia materia = null;

            OleDbConnection conex = null;

            try
            {
                conex = util.Conexion.conectarAccess();

                if (null != conex)
                {

                    string sql = "SELECT mat_id, mat_nombre, car_id" +
                                    " FROM mat_materias mat " +
                                    "WHERE mat_id = @mat_id";

                    OleDbCommand consulta = new OleDbCommand(sql, conex);

                    conex.Open();

                    consulta.Parameters.AddWithValue("@mat_id", mat_id);

                    OleDbDataReader lector = consulta.ExecuteReader();

                    while (lector.Read())
                    {
                        int id = Convert.ToInt32(lector[0].ToString());
                        string nombre = lector[1].ToString();
                        int car_id = Convert.ToInt32(lector[2].ToString());

                        materia = new Materia(id, nombre, car_id);
                    }

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                materia = null;
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return materia;
        }
    }
}
